package reno.eureka.timecycle.common;

import com.alibaba.fastjson.JSON;
import reno.eureka.timecycle.entity.CycleMO;
import reno.eureka.timecycle.entity.PersistentCycleMO;

public class ScheduleService implements Runnable {

    private String url;
    private String type;

    public ScheduleService(String url, String type) {
        this.url = url;
        this.type = type;
    }

    @Override
    public void run() {
        CycleMO cycleMO = null;
        boolean run = true;
        switch (type) {
            case "Cetus":
                while (run) {
                    System.out.println("Start Get CetusTime");
                    cycleMO = getTimeCycle();
                    if (cycleMO != null) {
                        correctiveData(cycleMO, 1);
                        System.out.println(type + " Finish");
                        try {
                            Thread.sleep(1800000);
                        } catch (InterruptedException e) {
                            System.out.println("CetusTime Sleep Exception");
                        }
                    }
                }
                break;
            case "Earth":
                while (run) {
                    System.out.println("Start Get EarthTime");
                    cycleMO = getTimeCycle();
                    if (cycleMO != null) {
                        correctiveData(cycleMO, 2);
                        System.out.println(type + " Finish");
                        try {
                            Thread.sleep(86400000);
                        } catch (InterruptedException e) {
                            System.out.println("EarthTime Sleep Exception");
                        }
                    }
                }
                break;
            case "Vallis":
                while (run) {
                    System.out.println("Start Get VallisTime");
                    cycleMO = getTimeCycle();
                    if (cycleMO != null) {
                        correctiveData(cycleMO, 3);
                        System.out.println(type + " Finish");
                        try {
                            Thread.sleep(60000);
                        } catch (InterruptedException e) {
                            System.out.println("VallisTime Sleep Exception");
                        }
                    }
                }
                break;
        }
    }

    private CycleMO getTimeCycle() {
        return JSON.parseObject(HttpGetTool.getResult(url), CycleMO.class);
    }

    private void correctiveData(CycleMO cycleMO, int type) {
        cycleMO.pretreatment();
        PersistentCycleMO persistentCycleMO = PersistentCycleMO.getInstance();
        CycleMO oldCycleMO;
        switch (type) {
            case 1:
                oldCycleMO = persistentCycleMO.getCetusCycle();
                if (oldCycleMO == null || Long.valueOf(oldCycleMO.getId()) < Long.valueOf(cycleMO.getId())) {
                    persistentCycleMO.setCetusCycle(cycleMO);
                }
                break;
            case 2:
//                oldCycleMO = persistentCycleMO.getEarthCycle();
//                if (Long.valueOf(oldCycleMO.getId()) < Long.valueOf(cycleMO.getId())) {
//                    oldCycleMO = persistentCycleMO.getCetusCycle();
//                }
                persistentCycleMO.setEarthCycle(cycleMO);
                break;
            case 3:
                oldCycleMO = persistentCycleMO.getVallisCycle();
                if (oldCycleMO == null || Long.valueOf(oldCycleMO.getId()) < Long.valueOf(cycleMO.getId())) {
                    persistentCycleMO.setVallisCycle(cycleMO);
                }
                break;
        }

    }
}
